<template>
  <div class="star">
    <el-table
        :data="
        tableData.filter(
          (data) =>
            !search || data.tname.toLowerCase().includes(search.toLowerCase())
        )"
        style="width: 100%"
      highlight-current-row
      show-header
      @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="ID" prop="tid"> </el-table-column>
      <el-table-column label="鲜花类型" prop="tname"> </el-table-column>
      <el-table-column label="数量统计" prop="num"> </el-table-column>
      <el-table-column align="right">
        <template slot="header">
          <el-input v-model="search" size="mini" placeholder="输入鲜花类型搜索您想要的东西" />
        </template>
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              @click="disUpdate(scope.row)"
          >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
 
    <div class="footer">
  <div class="elbutton">
    <el-button type="success" plain size="small" @click="add">新增</el-button>
    <el-button type="danger" plain size="small" @click="deleteMore">批量删除</el-button>
  </div>

  <!-- 分页 -->
  <div class="block">
   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size=pageSize
      layout="total, sizes, prev, pager, next, jumper"
      :total=total
    >
    </el-pagination>
  </div>

  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="类型名称">
          <el-input v-model="form.tname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

 </div>
 
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      title: '',
      tableData: [],
      form: {
        id: null,
        tname: '',
      },
      search: "",
      radio: "",
      tname:'',
      type:'',
      dialogFormVisible: false,
      currentPage: 1,
      pageSize:10,
      total:0,
      multipleSelection:[]
    };
  },
  watch: {},
  computed: {},
  methods: {
    disUpdate(row) {
      this.title = '编辑'
      this.form.id = row.id
      this.form.tname = row.tname
      this.dialogFormVisible = true
    },
    // 新增的弹窗
    add() {
      this.title = '新增'
      this.form = {
        id: null,
        tname: ''
      }
      this.dialogFormVisible = true
    },
    // 新增的确定按钮
    save() {
      this.request.post("/type", this.form).then(res => {
        console.log(res)
        if (res.data.code === '200') {
          this.$message.success("恭喜小主！保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('小主！您的操作将在类型列表删除该类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.request.delete('/type/'+row.id).then(res=>{
          if(res.data.code ==="200"){
            this.$message.success("已听从您的指示！为您净化空间啦！")
            this.load()
           }
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '还好还好！可以挽回！已为您取消删除该订单'
        });
      });

    },
    deleteMore(){
     let ids = this.multipleSelection.map(v =>v.id)
      this.$confirm('小主！您的操作将在鲜花类型批量删除这些类型, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      center: true
    }).then(() => {
      this.request.post('/type/del/batch',ids).then(res=>{
        if(res.data.code ==="200"){
          this.$message.success("已听从您的指示！为您净化空间啦！")
          this.load()
         }
      })
    }).catch(() => {
      this.$message({
        type: 'warning',
        message: '还好还好！可以挽回！已为您取消删除这些好看的鲜花！'
      });
      // this.multipleSelection = []
    });
    },
  // 这是把选中的数据存到multipleSelection中
    handleSelectionChange(val) {
        this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.load()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.load()
    },
  
    // 页面初始化
    load() {
      this.request.get("/type/page", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          name:this.tname
        }
      }).then(res => {
        console.log(res)  
        this.tableData = res.data.data.records
        this.total = res.data.data.total
        console.log("@@@@@@@@@@",this.tableData)

      })
      this.request.get("/flower").then((res) => {
        this.tableData = res.data;
        
      });
    },
  },
  created() {
    this.load()
  },
  mounted() {
 
  },
};
</script>
<style lang="less" scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
.star {

  .compute {
    float: right;
    display: flex;
    // margin-bottom: 5%;
    // position: fixed; 是相对于浏览器窗口固定的。即使窗口滚动。他也不会移动
    position: fixed;
    top:650px;
    right:45px;

    .heji {
      // float: right;
      margin: 13px 50px;
      text-align: center;
      .price {
        color: red;
        font-size: 20px;
      }
    }
    .jiesuan {
      margin: 10px;
    }
  }
  .footer {
  display: flex;
  justify-content:space-between;
  .elbutton {
  padding-top: 15px;
}
.block {
  float: right;
  margin-top: 15px;
}
}
}
</style>